package com.hnkywl.sts.action.kcgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.kcgl.ZkdmxDao;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.xsgl.Psd;
import com.hnkywl.sts.entity.xsgl.Zkd;
import com.hnkywl.sts.service.kcgl.ZkdService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:17
 * To change this template use File | Settings | File Templates.
 */

@Namespace("/kcgl/zksq")
public class ZkdAction extends SimpleActionSupport {
    private ZkdService zkdService;
    //页面当前位置
    private int page;
    //每页行数
    private int rows;
    //行索引
    private int index;
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    //起始发货时间
    private String zkdh;
    //转库单ID
    private Long zkdId;
    //转库单
    private Zkd zkd;

    private Long zggsId;

    private ZkdmxDao zkdmxDao;

    private Psd zkps;

    private Long khId;

    @Action("zksq")
    public String zksq() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //领料单列表
    @Action("zkdList")
    public void ZkdList() {
        this.renderJson(this.zkdService.findAllZkd(zkdh, page, rows));
    }

    @Actions({@Action("zksq_content"), @Action("zksq_detail"), @Action("zksq_zkcontent"), @Action("zksq_zrcontent")})
    public String zkdContent() {
        if (zkdId != null) {
            zkd = this.zkdService.findZkdDetail(zkdId);
            zkps = zkd.getPsd();
        } else {
            zkd = new Zkd();
            zkps = new Psd();
        }
        this.zggsId = YgService.getCurrentYgZgGsId();
        return SUCCESS;
    }

    //转库单明细列表
    @Action("zkd_zkdmx")
    public void zkdmxDetail() {
        this.renderJson(this.zkdService.findZkdmxDetail(zkdId));
    }

    @Action("sskcZkdmx")
    public void sskcZkdmx() {
        this.renderJson(this.zkdService.findSskcZkdmxDetail(khId));
    }

    //转库申请
    @Action("add_zksq")
    public void addZkd() {
        try {
            this.zkdService.saveZkd(zkd);
            renderText("{\"statusCode\":\"200\", \"message\":\"转库单已保存！\", \"operationType\":\"add\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("zksq_content_print")
    public String zksqContentPrint() {
        if (zkdId != null) {
            zkd = zkdService.findZkdDetail(zkdId);
        } else {
            zkd = new Zkd();
        }
        return SUCCESS;
    }

    //修改转库单
    @Action("update_zksq")
    public void updateZksq() {
        try {
            this.zkdService.updateZkd(zkd);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改转库单成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    @Action("del_zksq")
    public void del_zksq() {
        try {
            this.zkdService.delZkd(zkdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"转库单已删除！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //提交转库申请
    @Action("tj_zksq")
    public void changeStatusTj() {
        try {
            zkdService.changeStatusTj(zkdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"转库单已提交！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    //审批转库申请
    @Action("sh_zksq")
    public void changeStatus() {
        try {
            zkdService.changeStatus(zkdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"转库单已审核！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    //转出
    @Action("zk_zksq")
    public void zcZksq() {
        try {
            this.zkdService.zk(zkd);
            renderText("{\"statusCode\":\"200\", \"message\":\"转库成功！\", \"operationType\":\"update\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //转入
    @Action("zk_zksq_zr")
    public void zrZksq() {
        try {
            this.zkdService.zr(zkd);
            renderText("{\"statusCode\":\"200\", \"message\":\"转库成功！\", \"operationType\":\"update\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("zkdgl_zkfy")
    public void zkfyDetail() {
        this.renderJson(this.zkdService.findZkfyDetail(zkdId));
    }

    @Action("zksq_print_detail")
    public void zksqPrintDetail() {
        Gson gson = new Gson();
        List<Map> zkdmxList = this.zkdmxDao.findZkdmxList(zkdId);
        Double zksl = 0d;
        String result = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
        StringBuffer sbTable = new StringBuffer();
        if (null != zkdmxList) {
            sbTable.append("<table id = \"zkdmxdetail\" class=\"tb intb\"><thead>");
            sbTable.append("<tr>");
            sbTable.append("<td>物资编码</td><td>物资名称</td><td>型号规格</td><td>单位</td><td>材质</td>" +
                    "  <td>应拨数量</td><td>单价</td><td>实拨出量</td><td>实拨入量</td>");
            sbTable.append("</tr></thead><tbody>");

            for (int i = 0; i < zkdmxList.size(); i++) {
                sbTable.append("<tr>");
                // 物资编码
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("jtwz.oldcode")) {
                    sbTable.append(zkdmxList.get(i).get("jtwz.oldcode"));
                }
                sbTable.append("</td>");
                //    品名
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("jtwz.name")) {
                    sbTable.append(zkdmxList.get(i).get("jtwz.name"));
                }
                sbTable.append("</td>");
                //  规格型号
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("jtwz.ggxh")) {
                    sbTable.append(zkdmxList.get(i).get("jtwz.ggxh"));
                }
                sbTable.append("</td>");
                //单位
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("jtwz.dw")) {
                    sbTable.append(zkdmxList.get(i).get("jtwz.dw"));
                }
                sbTable.append("</td>");
                //材质
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("jtwz.cz")) {
                    sbTable.append(zkdmxList.get(i).get("jtwz.cz"));
                }
                sbTable.append("</td>");
                // 应拨数量
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("zksl")) {
                    Double mzksl = (Double) zkdmxList.get(i).get("zksl");
                    BigDecimal bzksl = new BigDecimal(mzksl);
                    Double dzksl = bzksl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dzksl));
                    zksl += dzksl;
                }
                sbTable.append("</td>");
                //库存单价
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("wsdj")) {
                    Double mwsdj = (Double) zkdmxList.get(i).get("wsdj");
                    BigDecimal bwsdj = new BigDecimal(mwsdj);
                    Double dwsdj = bwsdj.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dwsdj));
                }
                sbTable.append("</td>");
                //实拨出量
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("zcsl")) {
                    Double mzcsl = (Double) zkdmxList.get(i).get("zcsl");
                    BigDecimal bzcsl = new BigDecimal(mzcsl);
                    Double dzcsl = bzcsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dzcsl));
                }
                sbTable.append("</td>");
                //实拨入量
                sbTable.append("<td>");
                if (null != zkdmxList.get(i).get("zrsl")) {
                    Double mzrsl = (Double) zkdmxList.get(i).get("zrsl");
                    BigDecimal bzrsl = new BigDecimal(mzrsl);
                    Double dzrsl = bzrsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dzrsl));
                }
                sbTable.append("</td>");

                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"5\">合计:");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(df.format(zksl));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</tbody></table>");
        }
        renderHtml(sbTable.toString());
    }

    //转库配送的跳转
    @Action("zksq_ps_content")
    public String zksqPsContent() {
        if (zkdId != null) {
            zkd = zkdService.findZkdDetail(zkdId);
        } else {
            zkd = new Zkd();
        }
        zkps = new Psd();
        return SUCCESS;
    }

    //新增转库配送单
    @Action("add_zkd_ps")
    public void addZkdPs() {
        try {
            this.zkdService.saveZkdPs(zkps);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加转库配送单成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    public Zkd getZkd() {
        return zkd;
    }

    public void setZkd(Zkd zkd) {
        this.zkd = zkd;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public GnczService getGnczService() {
        return gnczService;
    }

    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    public String getZkdh() {
        return zkdh;
    }

    public void setZkdh(String zkdh) {
        this.zkdh = zkdh;
    }

    public Long getZkdId() {
        return zkdId;
    }

    public void setZkdId(Long zkdId) {
        this.zkdId = zkdId;
    }

    public Long getZggsId() {
        return zggsId;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public void setZggsId(Long zggsId) {
        this.zggsId = zggsId;
    }

    public Psd getZkps() {
        return zkps;
    }

    public void setZkps(Psd zkps) {
        this.zkps = zkps;
    }

    @Autowired
    public void setZkdService(ZkdService zkdService) {
        this.zkdService = zkdService;
    }

    @Autowired
    public void setZkdmxDao(ZkdmxDao zkdmxDao) {
        this.zkdmxDao = zkdmxDao;
    }
}
